package com.zadtek.shopmall.activities;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsListActivity;
import com.zadtek.shopmall.bean.HomeBaseBean;
import com.zadtek.shopmall.bean.OperateResponseBean3;
import com.zadtek.shopmall.bean.ShopLikeItem;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LikeShopActivity extends AbsListActivity {
    private LikeAdapter likeAdapter;
    private List<ShopLikeItem> data = new LinkedList<>();

    private int page =1 ;
    private int pagesize = 10;

    @Override
    protected String getTopTitle() {
        return "关注的店铺";
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        likeAdapter = new LikeAdapter(R.layout.like_shop_item,data);
        likeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.tv_btn_cancel){
                    cancelLikeRequest(position);
                }
            }
        });
        return likeAdapter;
    }

    private void cancelLikeRequest(final int position) {
        Map<String, Object> param = NetTool.newParams();
        param.put("store_id",data.get(position).getRu_id());
        NetTool.getApi().delLikeShop(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OperateResponseBean3>>() {
                    @Override
                    public void onData(BaseResponse<OperateResponseBean3> value) {
                        if(value.getCode() == 0 && value.getData().isSuccess()){
                            ToastUtil.showToast("操作成功！");
                            data.remove(position);
                            likeAdapter.notifyDataSetChanged();
                        }else{
                            ToastUtil.showToast(value.getData().getMessage());
                        }
                    }
                });
    }

    @Override
    public void loadMore() {
        requestData();
    }

    @Override
    protected void requestData() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("uid",67);
        param.put("pagesize",pagesize);
        NetTool.getApi().getLikeShops(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<ShopLikeItem>>>() {
                    @Override
                    public void onData(BaseResponse<List<ShopLikeItem>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().size() != 0){
                                data.addAll(value.getData());
                                likeAdapter.notifyDataSetChanged();
                                likeAdapter.loadMoreComplete();
                                page++;
                            }else{
                                likeAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    private class LikeAdapter extends BaseQuickAdapter<ShopLikeItem,BaseViewHolder>{

        public LikeAdapter(int layoutResId, @Nullable List<ShopLikeItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ShopLikeItem item) {
            helper.addOnClickListener(R.id.tv_btn_cancel);
            helper.setText(R.id.tv_shop_name,item.getShop_name());
            helper.setText(R.id.tv_shop_likes,"已有"+item.getOnFocus()+"人关注");
            Glide.with(mActivity).load(item.getShop_logo()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_shop_img));
        }
    }

    @Override
    public void OnItemClick(int position) {
        HomeBaseBean baseBean = new HomeBaseBean();
        baseBean.setType(Const.SHOP);
        baseBean.setId(data.get(position).getRu_id());
        Const.jumpHandle(LikeShopActivity.this,baseBean);
    }
}
